<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;

class CompanySearch extends ApiModel
{
    //
    protected $table = 'company_search';

    protected $guarded = [];

    protected $hidden = ['uid', 'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4',
        'jobsort5', 'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5', 'created_at',
        'census_area_name', 'homeaddress_area_name'];

    /**
     * The attributes that should be casted to native types.
     * 转换属性
     * @var array
     */
    protected $casts = [
        'id' => 'string',
        'uid' => 'string',
        'chkphoto_open' => 'string',
    ];

    /**
     * 获取职位id数组
     *
     * @return array
     */
    public function getJobsortAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobsort' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取职位名
     *
     * @return string
     */
    public function getJobsortNameAttribute()
    {
        return implode(',', \DB::table('coll_position')
            ->whereIn('cid', $this['jobsort'])->pluck('name'));
    }

    /**
     * 获取地区id数组
     *
     * @return array
     */
    public function getJobareaAttribute()
    {
        $arr = [];

        //遍历地区
        for ($i = 1; $i <= 5; $i++) {
            $value = $this['jobarea' . $i];
            if ($value != 0) {
                $arr[] = $value;
            }
        }
        return $arr;
    }

    /**
     * 获取地区名
     *
     * @return string
     */
    public function getJobareaNameAttribute()
    {
        $area = [];
        $town = [];
        foreach ($this->jobarea as $item) {
            if (strlen(strval($item)) > 6) {
                $town[] = $item;
            } else {
                $area[] = $item;
            }
        }
        $areaName = [];
        if (count($area) > 0) {
            $areaName = CollArea::whereIn('cid', $area)->pluck('name', 'cid')->toArray();
        }
        $townName = [];
        if (count($town) > 0) {
            $townName = CollTown::whereIn('cid', $town)->pluck('name', 'cid')->toArray();
        }

        $arr = $areaName + $townName;
        $str = '';
        //遍历地区
        foreach ($this->jobarea as $item) {
            if ($item != 0 && array_key_exists($item, $arr)) {
                $str .= $arr[$item] . ',';
            }
        }
        return rtrim($str, ',');
    }

    /**
     * 获取学历名
     *
     * @return string
     */
    public function getEducationNameAttribute()
    {
        $eduId = $this->attributes['education'];
        if ($eduId == 0) {
            return '学历不限';
        }
        return CollOption::getOptionName($eduId, 'education');
    }

    /**
     * 获取工作经验名
     *
     * @return string
     */
    public function getWorkYearNameAttribute()
    {
        $workId = $this->attributes['work_year'];
        if ($workId == 0) {
            return '经验不限';
        }
        return CollOption::getOptionName($workId, 'work_year');
    }

    /**
     * 获取现住址名
     *
     * @return string
     */
    public function getHomeaddressNameAttribute()
    {
        return $this->homeaddress_area_name ? $this->homeaddress_area_name->name : '';
    }

    public function homeaddress_area_name()
    {
        return $this->hasOne(CollArea::class, 'cid', 'homeaddress')
            ->select('cid', 'name');
    }

    /**
     * 获取户籍名
     *
     * @return string
     */
    public function getCensusNameAttribute()
    {
        return $this->census_area_name ? $this->census_area_name->name : '';
    }

    public function census_area_name()
    {
        return $this->hasOne(CollArea::class, 'cid', 'census')
            ->select('cid', 'name');
    }

    public function getUpdatetimeNameAttribute()
    {
        $id = $this->attributes['updatetime'];
        if ($id == 0) {
            return '';
        }
        return CollOption::getOptionName($id, 'release_date');
    }

    public function getAgeNameAttribute()
    {
        $id = $this->attributes['age'];
        if ($id == 0) {
            return '年龄不限';
        }
        return CollOption::getOptionName($id, 'age_stint');
    }
}
